/*
 * @title: 粘贴板工具
 * @author: andy.ten@tom.com & xukaixing@hotmail.com
 * @Date: 2022-04-06 16:50:39
 * @LastEditors: andy.ten@tom.com
 * @LastEditTime: 2022-04-25 10:58:18
 * @version: 1.0.2
 * @copyright: copyright (c) 2022 andyten
 *  node: you can not copy and/or use and/or modify this program free,
 *  please reserve the segment above.Please mail me if you have any question.
 */
import Vue from 'vue';
import Clipboard from 'clipboard';

function clipboardSuccess() {
  Vue.prototype.$message({
    message: '粘贴成功',
    type: 'success',
    duration: 3000
  });
}

function clipboardError() {
  Vue.prototype.$message({
    message: '粘贴失败',
    type: 'error',
    duration: 5000
  });
}

export default function handleClipboard(text, event) {
  const clipboard = new Clipboard(event.target, {
    text: () => text
  });
  clipboard.on('success', () => {
    clipboardSuccess();
    clipboard.destroy();
  });
  clipboard.on('error', () => {
    clipboardError();
    clipboard.destroy();
  });
  clipboard.onClick(event);
}
